
import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('MovingScengcebg')
export class MovingScengcebg extends Component {
    // [1]
    @property(Node)
    bg01:Node=null;

    @property(Node)
     bg02:Node=null;

     private _bgSpeed=10;
     private _bgMoveingRange=90;

    start ()
    {
     this._init();

    }

    //deltaTime  每帧更新的时间
    update (deltaTime: number) 
    {
        this._moveBackground(deltaTime);
    }

    test()
    {

        this._init();
    }
    private _init()
    {
        this.bg01.setPosition(0,0,0);
        this.bg02.setPosition(0,0,-this._bgMoveingRange);
    }

    private _moveBackground(deltaTime:number)
    {
            this.bg01.setPosition(0,0,this.bg01.position.z+this._bgSpeed*deltaTime); 
            this.bg02.setPosition(0,0,this.bg02.position.z+this._bgSpeed*deltaTime);

             if(this.bg01.position.z>this._bgMoveingRange)
             {
                   this.bg01.setPosition(0,0,this.bg02.position.z-this._bgMoveingRange)
             }
             else if(this.bg02.position.z>this._bgMoveingRange)
             {
                this.bg02.setPosition(0,0,this.bg01 .position.z-this._bgMoveingRange)

             }

    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.0/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.0/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.0/manual/en/scripting/life-cycle-callbacks.html
 */
